package cn.ee.dfs.wx;

import cn.ee.dfs.common.controller.BaseController;
import cn.ee.dfs.common.entity.DfsResponse;
import cn.ee.dfs.common.exception.DfsException;
import cn.ee.dfs.common.utils.NetTool;
import cn.ee.dfs.esb.entity.UserService;
import cn.ee.dfs.system.entity.User;
import cn.ee.dfs.system.service.IUserService;
import cn.ee.dfs.wx.model.WxUser;
import cn.ee.dfs.wx.service.IWxuserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Validated
@RestController
@RequestMapping("/wx/api")
public class WxLoginController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IWxuserService wxuserService;

    @PostMapping("onLogin")
//    @Limit(key = "login", period = 60, count = 20, name = "登录接口", prefix = "limit")
    public DfsResponse onLogin(@RequestBody Map<String, Object> data) throws DfsException {
        //封装请求体
        Map<String, String> paramMap = new HashMap<>();
        String code = data.get("code").toString();
        paramMap.put("code", code);
        String url = WXContent.URL_CODE_TO_SESSION.replace("JSCODE", code).replace("SECRET", WXContent.APP_SECRET).replace("APPID", WXContent.APPID);
        System.out.println("url = onLogin==>" + url);
        String result = NetTool.post(url, paramMap);
//        https://api.weixin.qq.com/sns/jscode2session?appid=wx7bb3d751bd6083bd&secret=567c0b8e5dc17418cec41ab8b57ea90a&js_code=0d1H1DFa19IPJF02plFa18uqfY0H1DFv&grant_type=authorization_code
//        {"session_key":"A+2gL+8hhyATm3zkruKxxg==","openid":"oqLyQ641dIHp7B87U5XbePxjdkrI"}
        System.out.println("---onLogin--result-->" + result);

        JSONObject loginInfo = JSON.parseObject(result);
        String sessionKey = loginInfo.getString("session_key");
        String openid = loginInfo.getString("openid");

        WxUser user = wxuserService.getOne(Wrappers.lambdaQuery(new WxUser(openid)).last("LIMIT 1"));
        System.out.println("-- onLogin--user =>" + user);

        if (Objects.isNull(user)) {
            // new user
            user = new WxUser();
            user.setCreateTime(new Date());
            user.setStatus(WxUser.STATUS_VALID);
        }
        user.setOpenId(openid);
        user.setSessionKey(sessionKey);
        user.setModifyTime(new Date());
        wxuserService.saveOrUpdate(user);

        return new DfsResponse().data(user).success();
    }

    @PostMapping("upload2")
    public String test13(@RequestBody JSONObject map) {
        String a = (String) map.get("a");
        String b = (String) map.get("b");
        return "i from server!!!JSONObject->" + a + b;
    }


}
